package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.entity.AnimaAccess;
import com.shoujifeng.snowmusic.entity.Mp3Info;
import com.shoujifeng.snowmusic.player.R.anim;
import com.shoujifeng.snowmusic.player.interfaces.MoreInterface;
import com.shoujifeng.snowmusic.player.utils.MusicPlayer;

/*
 * 歌单音乐列表界面
 * */
public class MusicMenuContentActivity extends Activity {
	private ListView listView;
	private List<HashMap<String, Object>> mp3Infos;
	private LinearLayout musicPlayBtn;
	private LinearLayout musicAddBtn;
	private String name;
	private MyAdapter adapter;
	public static String formActivity = ""; // 判断传到播放器中的activity
	private RelativeLayout headView;
	private TextView headText;
	private ImageButton breakBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_menu_content);

		init();
		setListView();
		setOnClick();
	}

	private void init() {
		listView = (ListView) findViewById(R.id.music_menu_list);
		musicPlayBtn = (LinearLayout) findViewById(R.id.music_menu_play);
		musicAddBtn = (LinearLayout) findViewById(R.id.music_menu_add);
		name = getIntent().getExtras().getString("name");
		headView = (RelativeLayout) findViewById(R.id.headLay);
		headText = (TextView) headView.findViewById(R.id.head_text);
		headText.setText(name);
		breakBtn = (ImageButton) headView.findViewById(R.id.breakBtn);
		listView.setOnItemClickListener(itemClickListener);
	}
 
	private void setListView() {
		DatabaseUtil databaseUtil = new DatabaseUtil(
				MusicMenuContentActivity.this);
		mp3Infos = databaseUtil.queryMusicMenuList(name);
		adapter = new MyAdapter(this, mp3Infos, R.layout.native_music_list,
				new String[] { "musicName", "playerName" }, new int[] {
						R.id.musicName, R.id.playerName });
		listView.setAdapter(adapter);

	}

	private void setOnClick() {
		musicAddBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MusicMenuContentActivity.this,
						AddToMenuActivity.class);
				intent.putExtra("name", name);
				NativeMusicManageActivity.activityName = "MusicMenuContentActivity";
				startActivity(intent);
				
				overridePendingTransition(R.anim.right_in, R.anim.left_out);
			}
		});

		musicPlayBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MusicMenuContentActivity.this,
						MusicPlayerActivity.class);
				formActivity = "MusicMenuContentActivity";
				MusicPlayer musicPlayer = new MusicPlayer(
						MusicMenuContentActivity.this);
				List<Mp3Info> mp3Infos = musicPlayer
						.getMp3Infos(MusicMenuContentActivity.this);
				MusicPlayer.musicInfos = mp3Infos;
				Random random = new Random();
				int s = random.nextInt(mp3Infos.size())
						% (mp3Infos.size() - 0 + 1) + 0;
				MusicPlayer.position = s;
				startActivity(intent);
				
				overridePendingTransition(R.anim.bottom_in,android.R.anim.fade_out);
			}
		});
		breakBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				
				overridePendingTransition(R.anim.no_move, R.anim.right_out);
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}

	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view,
				int position, long arg3) {
			// 此处相应ListView 项按下
			MusicPlayerActivity.isOnline = false;
			Intent intent = new Intent(MusicMenuContentActivity.this,
					MusicPlayerActivity.class);
			formActivity = "MusicMenuContentActivity";
			MusicPlayer musicPlayer = new MusicPlayer(
					MusicMenuContentActivity.this);
			List<Mp3Info> mp3Infos = musicPlayer
					.getMp3Infos(MusicMenuContentActivity.this);
			MusicPlayer.musicInfos = mp3Infos;
			MusicPlayer.position = position;
			startActivity(intent);
			
			overridePendingTransition(R.anim.bottom_in,android.R.anim.fade_out);
		}
	};

	private class MyAdapter extends BaseAdapter {
		int count = 0;
		private List<HashMap<String, Object>> mItemList;
		//上一次点击的MoreView
		private View mLastMore;
		private int layout;
		private String[] mKeys;
		private int[] ids;
		private Context mContext;
		
		public MyAdapter(Context context,
				List<HashMap<String, Object>> arrayList, int songMenuList,
				String[] strings, int[] is) {
			
			//super(context, arrayList, songMenuList, strings, is);
			mItemList = arrayList;
			if (arrayList == null) {
				count = 0;
			} else {
				count = arrayList.size();
			}
			// TODO Auto-generated constructor stub
			
			layout = songMenuList;
			mKeys = strings;
			ids = is;
			mContext = context;
			
			mLastMore = null;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mItemList.size();
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			convertView = LayoutInflater.from(mContext).inflate(layout, null);
			
			HashMap<String, Object> map = mItemList.get(position);
			final String musicName = (String) map.get("musicName");
			final String playerName = (String) map.get("playerName");
			
			//View view = super.getView(position, convertView, parent);
			TextView musicNameText = (TextView) convertView.findViewById(R.id.musicName);
			TextView playerNameText = (TextView) convertView.findViewById(R.id.playerName);
			musicNameText.setText(musicName);
			playerNameText.setText(playerName);
			
			ImageView imgMore = (ImageView) convertView.findViewById(R.id.moreImg);
			
			//设置弹出菜单
			if(imgMore != null)
			{ 
				//得到弹出菜单， 将Pos储存
				View moreView = (View)convertView.findViewById(R.id.music_more_layout);
				moreView.setTag(position);
				
				//将弹出菜单存到按钮里
				imgMore.setTag(moreView);
				imgMore.setOnClickListener(moreOnClickListener);
				
				//隐藏所有弹出菜单
				moreView.setVisibility(View.GONE);
				if(mLastMore != null)
				{	
					//mLastMore.setVisibility(View.GONE);
					//mLastMore = null;
				}
				
				//添加相关点击
				Button btnAdd = (Button)moreView.findViewById(R.id.native_music_btn_add);
				Button btnDel = (Button)moreView.findViewById(R.id.native_music_btn_del);
				btnAdd.setTag(position);
				btnDel.setTag(position);
				btnAdd.setOnClickListener(AddOnClickListener);
				btnDel.setOnClickListener(DelOnClickListener);
			}
			
			return convertView;
		}
			
			//更多按下
			private View.OnClickListener moreOnClickListener = new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {

					View moreView = (View)v.getTag();
					
					//如果上一次点击了菜单， 隐藏之
					if(mLastMore != null) {
						mLastMore.setVisibility(View.GONE);
					}
					
					//滑动动画
					AnimaAccess anim = new AnimaAccess(MusicMenuContentActivity.this);
					moreView.startAnimation(anim.alphaAnimation(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 200, false));
					
					moreView.setVisibility(View.VISIBLE);
					
					//储存这次按下的菜单
					mLastMore = moreView;
				}
			};
			
			//下载按下
			private View.OnClickListener AddOnClickListener = new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					int position = Integer.parseInt(v.getTag().toString());
					
					//moreInterface.OnClickDownload(position);
					
					HashMap<String, Object> map = mp3Infos.get(position);
					EditListMusicActivty.mp3list = new ArrayList<HashMap<String, Object>>();
					EditListMusicActivty.mp3list.add(map);
					
					EditListMusicActivty.noList = new ArrayList<String>();
					EditListMusicActivty.noList.add(0 + "");
					
					Intent intent = new Intent(MusicMenuContentActivity.this,EditAddTo.class);
					startActivity(intent);
					
					mLastMore.setVisibility(View.GONE);
				}
			};
			
			//删除按下
				private View.OnClickListener DelOnClickListener = new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						int position = Integer.parseInt(v.getTag().toString());
						
						dialog(position);
						
						mLastMore.setVisibility(View.GONE);
					}
				};
				
				  protected void dialog(final int position) { 
				        AlertDialog.Builder builder = new Builder(MusicMenuContentActivity.this); 
				        builder.setMessage("确定要删除该歌曲吗?"); 
				        builder.setTitle("提示"); 
				        builder.setPositiveButton("删除", 
				                 
				                new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {
										
										HashMap<String, Object> map = mp3Infos.get(position);
										
										DatabaseUtil db = new DatabaseUtil(MusicMenuContentActivity.this);
										db.deleteMusicMenu(Integer.parseInt(map.get("index").toString()));
										
										mp3Infos.remove(position);
										
										notifyDataSetChanged();
										
										Toast.makeText(MusicMenuContentActivity.this, "删除成功!", Toast.LENGTH_LONG).show();
									}
				        	
				        }); 
				        builder.setNegativeButton("取消", null); 
				        builder.create().show(); 
				    } 
	}
}
